#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    :  
# @function: 
# @version :


import asyncio
import requests


async def get_image(url):
    # 发送网络请求获取图片
    print('开始下载：', url)

    # 获取事件循环
    loop = asyncio.get_event_loop()

    # 创建线程池运行
    future = loop.run_in_executor(None, requests.get, url)
    response = await future

    file_name = url.rsplit("/")[-1]
    save_path = "img/" + file_name
    with open(save_path, 'wb') as f:
        f.write(response.content)


url_list = [
    'http://pic.bizhi360.com/bbpic/98/10798.jpg',
    'http://pic.bizhi360.com/bbpic/92/10792.jpg',
    'http://pic.bizhi360.com/bbpic/86/10386.jpg'
]

# 直接将协程对象放入到事件循环中
tasks = [get_image(url) for url in url_list]

asyncio.run(asyncio.wait(tasks))
